app.controller('brandController', function ($scope, $controller,brandService) {
    $controller('baseController', {$scope: $scope});
    $scope.findAll = function () {
        brandService.findAll().success(function (response) {
            $scope.list = response;
        });
    };
    $scope.findByPage = function (pageNum, pageSize) {
        brandService.findByPage(pageNum, pageSize).success(function (response) {
            $scope.list = response.rows;
            $scope.reload = false;
            $scope.paginationConf.totalItems = response.total;
            setTimeout(function () {
                $scope.reload = true;
            }, 100);
        });
    };
    $scope.findById = function (id) {
        brandService.findById(id).success(function (response) {
            $scope.entity = response;
        });
    };
    $scope.search = function (pageNum, pageSize) {
        brandService.search(pageNum, pageSize, $scope.searchEntity).success(function (response) {
            $scope.list = response.rows;
            $scope.reload = false;
            $scope.paginationConf.totalItems = response.total;
            setTimeout(function () {
                $scope.reload = true;
            }, 100);
        });
    };
    $scope.save = function () {
        if ($scope.entity.id != null) {
            brandService.update($scope.entity).success(function (response) {
                if (response.success) {
                    $scope.reloadList();
                } else {
                    alert(response.message);
                }
            });
        } else {
            if ($.isEmptyObject($scope.entity)) {
                alert("表单不能为空");
            }else{
                brandService.save($scope.entity).success(function (response) {
                    if (response.success) {
                        $scope.paginationConf.totalItems = $scope.paginationConf.totalItems + 1;
                    } else {
                        alert(response.message);
                    }
                });
            }
        }
    };
    $scope.delete = function () {
        var ids = $.param($scope.checkedIds, true)
        brandService.delete(ids).success(function (response) {
            if (response.success) {
                $scope.paginationConf.totalItems = $scope.paginationConf.totalItems - $scope.checkedIds.length;
                $scope.checkedIds.ids = [];
            } else {
                alert(response.message);
            }
        });
    };
});